//
//  PingTeHeaderView.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/26.
//

#import "PingTeHeaderView.h"
#import "CommonUtil.h"
#import "PingteAccuracyView.h"

@interface PingTeHeaderView()

@property (strong, nonatomic) UIImageView * bgImageView;
@property (strong, nonatomic) UIImageView * iconImageView;
@property (strong, nonatomic) PingteAccuracyView * accuracyView;
@property (strong, nonatomic) UILabel * nameLabel;
@property (strong, nonatomic) UIButton * wordBtn;
@property (strong, nonatomic) UIButton * zanBtn;
@property (strong, nonatomic) UIView * lineView;
@property (strong, nonatomic) UIView * tabView;
@property (strong, nonatomic) UIButton * lastSelect;
@property (strong, nonatomic) UIButton * lastTitleBtn;
@property (strong, nonatomic) UIView * indexView;

@property (strong, nonatomic) UIView * tempView;

@end

@implementation PingTeHeaderView


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.bgImageView];
        [self addSubview:self.iconImageView];
        [self addSubview:self.accuracyView];
        [self addSubview:self.nameLabel];
        [self addSubview:self.wordBtn];
        [self addSubview:self.zanBtn];
        [self addSubview:self.tabView];
        [self.tabView addSubview:self.lineView];
        [self.tabView addSubview:self.indexView];
        [self.tabView addSubview:self.tempView];
        
        NSArray *btns = @[@"特肖",@"特码",@"平特",@"半数",@"其他",@"杀榜"];
        CGFloat w  = SCREEN_WIDTH/btns.count;
        for (NSInteger i = 0; i<btns.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitle:btns[i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn setTitleColor:RGB(255.0, 83.0, 133.0) forState:UIControlStateSelected];
            btn.titleLabel.font = [UIFont systemFontOfSize:AD(15)];
            [self.tabView addSubview:btn];
            [btn addTarget:self action:@selector(tabBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            btn.frame = CGRectMake(i * w, 0, w, AD(50));
            if (i == 0) {
                [self tabBtnClick:btn];
            }
        }

        
    }
    return self;
}

- (void)refreshBtnWithTiels:(NSArray *)titles {
    CGFloat x = AD(20);
    CGFloat y = AD(10);
    
    
    for (UIView *view in self.tempView.subviews) {
        [view removeFromSuperview];
    }
    
    

    
    for (NSInteger i = 0; i < titles.count; i++) {
        NSString *title = titles[i];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:RGB(255.0, 83.0, 133.0) forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont systemFontOfSize:AD(15)];
        btn.layer.cornerRadius = AD(12.5);
        btn.layer.borderWidth = AD(1);
        btn.layer.borderColor = RGB(243.0, 243.0, 243.0).CGColor;
        btn.backgroundColor = RGB(243.0, 243.0, 243.0);
        CGFloat w = [CommonUtil countWidth:title font:btn.titleLabel.font height:AD(35)] + AD(30);
        btn.frame = CGRectMake(x, y, w, AD(25));
        x = btn.right + AD(5);
        if (x > (SCREEN_WIDTH )) {
            y = btn.bottom + AD(10);
            x = AD(20);
            btn.frame = CGRectMake(x, y, w, AD(25));
            x = btn.right + AD(5);
        }
        [self.tempView addSubview:btn];
        if (i == 0) {
            [self titleBtnClick:btn];
        }else if (i == (titles.count -1)){
            self.tempView.height = btn.bottom;
        }
        
    }
    if (titles.count == 0) {
        self.tempView.height = 0;
        self.tabView.height = self.tempView.bottom;
    }else{
        self.tabView.height = self.tempView.bottom + AD(10);
    }
    
    self.height = self.tabView.bottom;
    if (self.actionBlock) {
        self.actionBlock(self.lastSelect.titleLabel.text);
    }
    [self.tabView addSubview:self.lineView];
    [self.tabView addSubview:self.indexView];
}


- (void)wordBtnClick:(UIButton *)sender{
    NSLog(@"留言");
}

- (void)zanBtnClick:(UIButton *)sender{
    NSLog(@"赞");
}

- (void)tabBtnClick:(UIButton *)sender{
    self.lastSelect.selected = false;
    self.lastSelect = sender;
    self.lastSelect.selected = YES;
    [UIView animateWithDuration:0.25 animations:^{
        self.indexView.centerX = sender.centerX;
    }];
    NSArray *titles;
    if ([sender.titleLabel.text isEqualToString:@"特肖"]) {
        titles = @[@"平特一肖",@"平特一尾",@"五不中",@"七不中",@"三中一",@"二连肖",@"二连尾"];
    }else if ([sender.titleLabel.text isEqualToString:@"特码"]){
        titles = @[@"平特一肖",@"平特一尾",@"五不中",@"七不中",@"三中一",@"二连肖"];
    }else if ([sender.titleLabel.text isEqualToString:@"平特"]){
        titles = @[@"平特一肖",@"平特一尾",@"五不中",@"七不中",@"三中一"];
    }else if ([sender.titleLabel.text isEqualToString:@"半数"]){
        titles = @[@"平特一肖",@"平特一尾",@"五不中",@"七不中"];
    }else if ([sender.titleLabel.text isEqualToString:@"其他"]){
        titles = @[@"平特一肖",@"平特一尾",@"五不中"];
    }else if ([sender.titleLabel.text isEqualToString:@"杀榜"]){
        titles = @[];
    }
    [self refreshBtnWithTiels:titles];
}

- (void)titleBtnClick:(UIButton *)sender{
    self.lastTitleBtn.selected = false;
    self.lastTitleBtn.layer.borderColor = RGB(243.0, 243.0, 243.0).CGColor;
    self.lastTitleBtn.backgroundColor = RGB(243.0, 243.0, 243.0);
    self.lastTitleBtn = sender;
    self.lastTitleBtn.selected = YES;
    self.lastTitleBtn.layer.borderColor = RGB(255.0, 83.0, 133.0).CGColor;
    self.lastTitleBtn.backgroundColor = [UIColor whiteColor];
}

#pragma mark - lazy
-(UIImageView *)bgImageView{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, AD(250))];
        _bgImageView.image = [UIImage imageNamed:@"forum_bg.jpg"];
    }
    return _bgImageView;
}

-(UIImageView *)iconImageView{
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"statistics_default.jpg"]];
        _iconImageView.frame = CGRectMake(AD(20), AD(40), AD(80), AD(80));
        _iconImageView.layer.cornerRadius = AD(40);
        _iconImageView.clipsToBounds = YES;
    }
    return _iconImageView;
}

-(PingteAccuracyView *)accuracyView{
    if (!_accuracyView) {
        CGFloat x = _iconImageView.right + AD(15);
        CGFloat w = SCREEN_WIDTH - x - AD(20);
        _accuracyView = [[PingteAccuracyView alloc] initWithFrame:CGRectMake(x, AD(30), w, AD(100))];
    }
    return _accuracyView;
}

-(UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(AD(20), _iconImageView.bottom + AD(20), AD(100), AD(25))];
        _nameLabel.font = [UIFont boldSystemFontOfSize:AD(18)];
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.text = @"吴妍珠";
    }
    return _nameLabel;
}

-(UIButton *)wordBtn{
    if (!_wordBtn) {
        _wordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _wordBtn.frame = CGRectMake(AD(12), _nameLabel.bottom + AD(5), AD(100), AD(30));
        _wordBtn.titleLabel.font = [UIFont systemFontOfSize:AD(15)];
        [_wordBtn setTitle:@"给TA留言" forState:UIControlStateNormal];
        [_wordBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_wordBtn setImage:[UIImage imageNamed:@"icon_word"] forState:UIControlStateNormal];
        _wordBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -AD(10));
        [_wordBtn addTarget:self action:@selector(wordBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _wordBtn;
}

-(UIButton *)zanBtn{
    if (!_zanBtn) {
        _zanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _zanBtn.frame = CGRectMake(_wordBtn.right, _wordBtn.top, AD(100), AD(30));
        _zanBtn.titleLabel.font = [UIFont systemFontOfSize:AD(15)];
        [_zanBtn setTitle:@"113获赞" forState:UIControlStateNormal];
        [_zanBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_zanBtn setImage:[UIImage imageNamed:@"icon_pingte_zan"] forState:UIControlStateNormal];
        _zanBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -AD(10));
        [_zanBtn addTarget:self action:@selector(zanBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _zanBtn;
}

-(UIView *)tabView{
    if (!_tabView) {
        _tabView = [[UIView alloc] initWithFrame:CGRectMake(0, AD(210), SCREEN_WIDTH, AD(0))];
        _tabView.backgroundColor = [UIColor whiteColor];
        _tabView.layer.cornerRadius = AD(10);
        _tabView.clipsToBounds = YES;
    }
    return _tabView;
}

-(UIView *)indexView{
    if (!_indexView) {
        _indexView = [[UIView alloc] initWithFrame:CGRectMake(0, _lineView.top, SCREEN_WIDTH/6, AD(1))];
        _indexView.backgroundColor = RGB(255.0, 83.0, 133.0);
    }
    return _indexView;
}

-(UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, AD(59), SCREEN_WIDTH, AD(1))];
        _lineView.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.3];
    }
    return _lineView;
}


-(UIView *)tempView{
    if (!_tempView) {
        _tempView = [[UIView alloc] initWithFrame:CGRectMake(0, AD(60), SCREEN_WIDTH, AD(0))];
        _tempView.backgroundColor = [UIColor whiteColor];
    }
    return _tempView;
}


@end
